//
//  HNSH_SendRedContent.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/8/12.
//

#import "HNSH_SendRedContent.h"

@interface HNSH_SendRedContent ()<RCMessageCoding, RCMessagePersistentCompatible, RCMessageContentView>

@end

@implementation HNSH_SendRedContent

+ (instancetype)objectWithCount:(NSString *)count price:(NSString *)price des:(NSString *)des {
    HNSH_SendRedContent * emoj = [[HNSH_SendRedContent alloc] init];
    if (emoj) {
        emoj.num = count;
        emoj.price = price;
        emoj.remark = des;
    }
    return emoj;
}

- (NSData *)encode{
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
    [dataDict setObject:self.num forKey:@"num"];
    [dataDict setObject:self.price forKey:@"price"];
    [dataDict setObject:self.remark forKey:@"remark"];
    [dataDict setObject:self.recordId forKey:@"id"];
    [dataDict setObject:@(self.status) forKey:@"status"];

    if (self.senderUserInfo) {
        NSMutableDictionary *userInfoDic = [[NSMutableDictionary alloc] init];
        if (self.senderUserInfo.name) {
            [userInfoDic setObject:self.senderUserInfo.name forKeyedSubscript:@"name"];
        }
        if (self.senderUserInfo.portraitUri) {
            [userInfoDic setObject:self.senderUserInfo.portraitUri forKeyedSubscript:@"portrait"];
        }
        if (self.senderUserInfo.userId) {
            [userInfoDic setObject:self.senderUserInfo.userId forKeyedSubscript:@"id"];
        }
        if (self.senderUserInfo.extra) {
            [userInfoDic setObject:self.senderUserInfo.extra forKeyedSubscript:@"extra"];
        }
        [dataDict setObject:userInfoDic forKey:@"user"];
    }
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dataDict options:NSJSONWritingPrettyPrinted error:nil];
    return jsonData;
}

- (void)decodeWithData:(NSData *)data{
    if (data) {
        __autoreleasing NSError *error = nil;
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        if (dic) {
            self.num = [NSString stringWithFormat:@"%@", dic[@"num"]];
            self.price = [NSString stringWithFormat:@"%@", dic[@"price"]];
            self.remark = [NSString stringWithFormat:@"%@", dic[@"remark"]];
            self.recordId = [NSString stringWithFormat:@"%@", dic[@"id"]];
            self.status = [dic[@"status"] integerValue];
            NSDictionary * userinfoDic = dic[@"user"];
            if (userinfoDic != nil) {
                [self decodeUserInfo:userinfoDic];
            }
        }
    }
}

+ (NSString *)getObjectName{
    return @"app:sendRedPacket";
}

+ (RCMessagePersistent)persistentFlag{
    return MessagePersistent_ISCOUNTED;
}

- (NSString *)conversationDigest{
    return @"[红包]";
}

@end
